package com.fuhe.chen.vendingmachine.service.impl;

import com.fuhe.chen.vendingmachine.dao.BasketDao;
import com.fuhe.chen.vendingmachine.dao.BasketDetailDao;
import com.fuhe.chen.vendingmachine.dto.CommodityInBasket;
import com.fuhe.chen.vendingmachine.pojo.Basket;
import com.fuhe.chen.vendingmachine.pojo.BasketDetail;
import com.fuhe.chen.vendingmachine.pojo.Commodity;
import com.fuhe.chen.vendingmachine.pojo.CommoditySpecification;
import com.fuhe.chen.vendingmachine.service.IBasketService;
import com.fuhe.chen.vendingmachine.service.ICommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.fuhe.chen.vendingmachine.constant.MessageConstant.ShoppingCart.ADD_REPEAT;
import static com.fuhe.chen.vendingmachine.constant.MessageConstant.ShoppingCart.ADD_SUCCESS;

@Service
public class BasketServiceImpl implements IBasketService {

    @Autowired
    BasketDao basketDao;

    @Autowired
    BasketDetailDao basketDetailDao;

    @Autowired
    ICommodityService commodityService;

    @Override
    @Transactional
    public Integer addCommodityToBasket(Integer commodity_id, Integer commodity_specification_id, Integer count, String user_id) {
        Basket basket = basketDao.findBasketByUserId(user_id);

        //检查是否在购物车表中已有记录
        if(basket==null){
            basket = new Basket();
            basket.setAdd_time(System.currentTimeMillis());
            basket.setUser_id(user_id);
            basketDao.addBasket(basket);
        }

        //检查在商品明细表中是否已有商品
        List<BasketDetail> goods = basket.getGoods();
        if(goods!=null&&goods.size()>0){
            for (BasketDetail good : goods) {
                if(good.getCommodity_id().equals(commodity_id)){
                    return ADD_REPEAT;
                }
            }
        }

        BasketDetail basketDetail = new BasketDetail();
        basketDetail.setCount(count);
        basketDetail.setCommodity_specification_id(commodity_specification_id);
        basketDetail.setCommodity_id(commodity_id);
        basketDetail.setBasket_id(basket.getId());
        basketDetailDao.addBasketDetail(basketDetail);

        return ADD_SUCCESS;

    }

    @Override
    public void deleteCommodityFromBasket(Integer basket_detail_id) {
        basketDetailDao.deleteBasketDetail(basket_detail_id);
    }

    @Override
    public List<CommodityInBasket> queryCommodityFromBasket(String user_id) {
        Basket basket = basketDao.findBasketByUserId(user_id);

        if(basket==null){
            return null;
        }
        List<BasketDetail> goods = basket.getGoods();
        if(goods==null||goods.size()==0){
            return null;
        }

        List<CommodityInBasket> result= new ArrayList<>();
        for (BasketDetail good : goods) {
            CommodityInBasket commodityInBasket = new CommodityInBasket();
            Commodity commodity = commodityService.queryOne(good.getCommodity_id());
            List<CommoditySpecification> specifications = commodity.getSpecifications();
            for (CommoditySpecification specification : specifications) {
                if(specification.getId().equals(good.getCommodity_specification_id())){
                    commodityInBasket.setSpecification(specification);
                }
            }
            commodityInBasket.setCommodity(commodity);
            commodityInBasket.setPicture_url(commodity.getPicture().get(0).getPicture_url());
            commodityInBasket.setBasket_detail_id(good.getId());

            result.add(commodityInBasket);
        }

        return result;
    }
}
